<?php
namespace Common\Model;
use Think\Model;

class UserModel extends Model{

	/**
	 *  获取单个用户信息 
	 */
	public function getUser($mobileNum="",$account="",$uid="",$where=array()){
		if (!empty($account)) {
			$where['account']=$account;
		}
		
		if (!empty($uid)) {
			$where['userID']=$uid;
		}
		
		if (!empty($mobileNum)) {
			$where['phone']=$mobileNum;
		}
		
		$result=$this->where($where)->find();
		
		return $result;
	}
	
	/**
	 *  添加单个用户信息
	 */
	public function addUser($mobileNum,$token,$channelID=null){
		$data=array(
				"phone"=>$mobileNum,
				"addTime"=>time(),
				"updateTime"=>time(),
				"lastLogin"=>time(),
				"lastIp"=>get_client_ip(),
				"token"=>$token,
				"channelID"=>$channelID
		);
		
		$add=$this->data($data)->add();
		
		return $add;
	}
	
	
	/**
	 *  更新单个用户信息
	 */
	public function updateUser($data){
		$update=$this->save($data);
	
		return $update;
	}
	
	
	/**
	 *  在缓存中通过uid获取旧token
	 */
	public function getOldToken($uid){
		$old=S("user_".$uid);
		
		return $old;
	}
	
	/**
	 *  将token写进缓存
	 */
	public function addToken($token,$str,$expire=0){
		$result=S("tkn_".$token,$str,$expire);
		
		return $result;
	}
	
	/**
	 *  通过token在缓存中读取用户信息
	 */
	public function getTokenUser($token){
		$user=S("tkn_".$token);
	
		return $user;
	}
	
	/**
	 *  修改缓存中uid的token
	 */
	public function editUserToken($uid,$token){
		$result=S("user_".$uid,$token);
		
		return $result;
	}
	
        /**
         * 获得列表（带基本查询）
         */
        public function getUserList($pageNo = 1, $pageSize = 15, $where = array(), $order = array(),$field="*") {
            $rt = $this->field($field)->where($where)->page("$pageNo,$pageSize")->order($order)->select();
            return $rt;
        }

        public function getUserListLength($where = array()) {
            return $this->where($where)->count();
        }
        
        public function getAllUserList($where = array(), $order = array(),$field="*") {
            $rt = $this->field($field)->where($where)->order($order)->select();
            return $rt;
        }
	
        /**
         * 将商店isOn属性置为0(即删除)
         */
        public function deleteUser($id) {
            if ($id <= 0) {
                $this->error = "无效ID";
                return false;
            }
            $data = array("isOn" => 0, "updateTime" => time());
            return $this->where("userID=$id")->save($data);
        }
        
}
?>